<%@page import="java.util.Date"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.poi.ss.usermodel.DateUtil"%>
<%@page import="org.apache.poi.ss.usermodel.Cell"%>
<%@page import="org.apache.poi.ss.usermodel.Row"%>
<%@page import="org.apache.poi.ss.usermodel.Sheet"%>
<%@page import="org.apache.poi.ss.usermodel.WorkbookFactory"%>
<%@page import="org.apache.poi.ss.usermodel.Workbook"%>
<%@page import="java.io.InputStream"%>
<%@ include file="Calendar_class.jsp" %>
<%!
/**
* 从Excel流中读取数据，并封装成集合
*/
public static List<Map<Integer,Object>> readExcelToList(InputStream is) throws Exception{
    Workbook wkbook = WorkbookFactory.create(is);//创建一个工作簿
    Sheet sheet = wkbook.getSheetAt(0);//获取一个表格
    List<Map<Integer,Object>> resultList = new ArrayList<Map<Integer,Object>>();//返回结果集
    int rowNum = sheet.getLastRowNum();//获取总行数
    for (int i = 1; i <= rowNum; i++) {
        Row row = sheet.getRow(i);//获取行
        if (row == null) {  
	        continue;  
	    }  
        int cellNum = row.getLastCellNum();//获取总列数
        Map<Integer,Object> m = new HashMap<Integer,Object>();
        for(int j=0;j<cellNum;j++){
            Cell cell = row.getCell(j);
            //resultMap.put(i+"_"+j,getCellFormatValue(cell));
            String val = getCellFormatValue(cell).trim();
            m.put(j,val);
        }
        resultList.add(m);
    }
    return resultList;
}



private static String getCellFormatValue(Cell cell){
    String val = "";
    if(null!=cell){
    	System.out.print(cell.getCellType());
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING ://字符
        	cell.setCellType(Cell.CELL_TYPE_STRING);
            val = (String)cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BLANK://空值
            val = "";
            break;
        case Cell.CELL_TYPE_NUMERIC://公式
            if (DateUtil.isCellDateFormatted(cell)) {
                Date d = DateUtil.getJavaDate(cell.getNumericCellValue()); 
                val = formatDate("yyyy-MM-dd HH:mm:ss",d);
            } else {// 纯数字
            	cell.setCellType(Cell.CELL_TYPE_STRING);
                val = (String)cell.getStringCellValue();
            } 
            break;
        case Cell.CELL_TYPE_FORMULA://公式
            if (DateUtil.isCellDateFormatted(cell)) {
                Date d = DateUtil.getJavaDate(cell.getNumericCellValue()); 
                val = formatDate("yyyy-MM-dd HH:mm:ss",d);
            } else {// 纯数字
                val = cell.getStringCellValue();
            } 
            break;
        }
    }
    return val;
}

//导出
%>